Skip to content

Conversation

@oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Nov 11, 2025

Implements functions that cannot be called at runtime (and thus also not from normal const functions, as those could be called from runtime).

This is done via the internal attribute rustc_comptime that can be added to normal functions, turning them into compile-time-only functions.

Because @fee1-dead and @compiler-errors did amazing work, we even get trait bounds that work inside comptime fns: via unconditionally-const const Trait bounds.

Use cases are

  • Reflection MVP #146923
  • const heap intrinsics
  • and the other various intrinsics (e.g. size_of 😆) that will just ICE in codegen or panic at runtime if they actually end up in runtime code

project goal issue: rust-lang/rust-project-goals#406

no tracking issue until we have a feature gate and some sort of syntax

cc @scottmcm as the T-lang goal champion

@rustbot
Copy link
Collaborator

rustbot commented Nov 11, 2025

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Nov 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 11, 2025

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Nov 11, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@jdonszelmann jdonszelmann self-assigned this Nov 11, 2025
@fee1-dead fee1-dead self-assigned this Nov 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to constck

cc @fee1-dead

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 21, 2025
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the comptime branch 2 times, most recently from da63f2f to 785eebe Compare November 25, 2025 11:35
@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 25, 2025

All reviews have been addressed

@bors
Copy link
Collaborator

bors commented Nov 26, 2025

☔ The latest upstream changes (presumably #149140) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Nov 27, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

}
}
} else if ccx.tcx.constness(callee) != hir::Constness::Const {
} else if ccx.tcx.constness(callee) != hir::Constness::Maybe {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit sus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What part? The rename or the inequality?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inequality. I feel like this should be if == hir::Constness::NotConst?

Comment on lines 4293 to 4295
pub fn is_const(&self) -> bool {
matches!(self.constness, Constness::Const)
matches!(self.constness, Constness::Maybe)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to audit usages of this. Leave a FIXME?

if should_encode_constness(def_kind) {
let constness = self.tcx.constness(def_id);
self.tables.constness.set(def_id.index, constness);
self.tables.constness.set(def_id.index, constness)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? (please revert)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase fallout

Comment on lines +235 to +237
( Maybe )
( Never )
( Always )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
( Maybe )
( Never )
( Always )
( Maybe )
( Never )
( Always )

let num_args = fields.len();
let func =
if context == hir::Constness::Const { called_in_const } else { called_at_rt };
if context == hir::Constness::Maybe { called_in_const } else { called_at_rt };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we shouldn't add another variant to hir::Constness given how this changes a lot of other things which might be a headache to update if we were to add comptime impls in the future, etc. Would it make sense to keep the hir:Constness::{,Not}Const, and extract the comptime enum specifically to a new enum? Might be a bit more work to do but worth it IMO, and might be better than doing it in a followup.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 28, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants